package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDTO;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {


    @Override
    public ResponseResult list(SensitiveDTO senseDto) {
        senseDto.checkParam();

        Page page = new Page(senseDto.getPage(), senseDto.getSize());

        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();
        String name = senseDto.getName();
        if (StringUtils.isNotBlank(name)) {
            wrapper.like(AdSensitive::getSensitives, name);
        }
        IPage iPage = this.page(page, wrapper);
        return new PageResponseResult(senseDto.getPage(), senseDto.getSize(), iPage.getTotal(), iPage.getRecords());
    }

    @Override
    public ResponseResult deleteById(Integer sid) {
        //1.检查参数
        if (sid == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdSensitive sieve = this.getById(sid);
        // 敏感词是否存在
        if (sieve == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "数据不存在");
        }
        this.removeById(sid);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult insetById(AdSensitive sensitive) {
        if (sensitive == null || StringUtils.isBlank(sensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST, "数据不存在");
        }
        // 判断数据库是否有
        int i = this.count(Wrappers.<AdSensitive>lambdaQuery()
                .eq(AdSensitive::getSensitives, sensitive.getSensitives()));
        if (i > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "敏感词已经存在");
        }
        // 设置时间
        sensitive.setCreatedTime(new Date());

        save(sensitive);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updateSensitive(AdSensitive sensitive) {
        Integer id = sensitive.getId();
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "id不能为空");
        }
        AdSensitive adSensitive = this.getById(id);
        if (adSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "敏感词不存在");
        }
        //设置修改时间
        sensitive.setCreatedTime(new Date());

        this.updateById(sensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Autowired
    AdSensitiveMapper adSensitiveMapper;

    /**
     * 查询敏感词内容列表
     */
    @Override
    public ResponseResult<List<String>> selectAllSensitives() {
        return ResponseResult.okResult(adSensitiveMapper.findAllSensitives());
    }
}
